package com.jd.controller;

import com.jd.common.Page;
import com.jd.pojo.User;
import com.jd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {


    @Autowired
    private UserService userService;


    @RequestMapping("/user/list")
    public String getUserList(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows, String username, Model model) {

        Page<User> pages = userService.selectUserList(page,rows,username);
        model.addAttribute("page", pages);
        model.addAttribute("username", username);
        return "userManage";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/user/delete")
    @ResponseBody
    public String deleteUser(Integer id){

        userService.deleteUser(id);
        return "ok";
    }


    /**
     * 修改时回显用户信息
     * @param id
     * @return
     */
    @RequestMapping("/user/edit")
    @ResponseBody
    public User editUser(Integer id){

        User user = userService.getUserInfo(id);
        return user;
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/user/update")
    @ResponseBody
    public String updateUser(User user) {
        userService.updateUser(user);
        return "ok";
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    @RequestMapping("/user/add")
    @ResponseBody
    public String addUser(User user){

        userService.addUser(user);
        return "ok";
    }



}
